﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMS.Database;
using System.Data;
using System.Collections;
using SIMS.Global;

namespace SIMS.Student
{
    public class StudentProfile
    {
        public int Id { get; set; }
        public Person.Profile Profile;
        public Person.Contact Contact;
        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

        public MyError Error { get; set; }


    }

    public class StudentProfileList : List<StudentProfile>
    {
        public MyError Error { get; set; }
        public StudentProfileList()
        {
        }
    }

    public class StudentProfileDB
    {
        MyDatabase _db = new MyDatabase();

        public StudentProfile SaveProfile(StudentProfile profile)
        {
            try
            {
                SortedList _param = new SortedList();
                _param.Add("@Id", profile.Id);

                if (profile.Profile.FirstName != string.Empty)
                {
                    _param.Add("@FirstName", profile.Profile.FirstName);
                }
                else
                {
                    _param.Add("@FirstName", DBNull.Value);
                }

                _param.Add("@LastName", profile.Profile.LastName);

                if (profile.Profile.Gender == 'M' || profile.Profile.Gender == 'F')
                {
                    _param.Add("@Gender", profile.Profile.Gender);
                }
                else
                {
                    _param.Add("@Gender", DBNull.Value);
                }

                if (DateTime.Compare(DateTime.MinValue, profile.Profile.DOB) < 0)
                {
                    _param.Add("@DOB", profile.Profile.DOB);
                }
                else
                {
                    _param.Add("@DOB", DBNull.Value);
                }

                if (profile.Profile.NRIC != string.Empty)
                {
                    _param.Add("@NRIC", profile.Profile.NRIC);
                }
                else
                {
                    _param.Add("@NRIC", DBNull.Value);
                }

                if (profile.Contact.ContactType > 0)
                {
                    _param.Add("@ContactType", Convert.ToByte(profile.Contact.ContactType));
                }
                else
                {
                    _param.Add("@ContactType", DBNull.Value);
                }

                if (profile.Contact.ContactNo != string.Empty)
                {
                    _param.Add("@ContactNo", profile.Contact.ContactNo);
                }
                else
                {
                    _param.Add("@ContactNo", DBNull.Value);
                }

                if (profile.Contact.Extension != string.Empty)
                {
                    _param.Add("@Extension", profile.Contact.Extension);
                }
                else
                {
                    _param.Add("@Extension", DBNull.Value);
                }

                if (profile.Contact.Email != string.Empty)
                {
                    _param.Add("@Email", profile.Contact.Email);
                }
                else
                {
                    _param.Add("@Email", DBNull.Value);
                }

                _param.Add("@IsActive", profile.IsActive);

                int _studentProfileID = _db.ExecuteSP("usp_StudentProfileInsertUpdateSingleItem", _param);

                if (_studentProfileID > 0)
                {
                    profile.Error.ErrorNo = 0;
                    profile.Id = _studentProfileID;
                }
                else
                {
                    profile.Error.ErrorNo = 1;
                    profile.Error.ErrorMessage = "No row affect when saving student's profile details.";
                }
            }
            catch (Exception Ex)
            {
                profile.Error = new MyError();
                profile.Error.ErrorNo = 10002;
                profile.Error.ErrorLocation = "StudentProfile";
                profile.Error.ErrorMessage = Ex.Message;
                profile.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "StudentProfile");
            }

            return profile;
        }

        public StudentProfileList GetProfileSearchResultList(string FullName)
        {
            StudentProfileList retValue = new StudentProfileList();

            try
            {
                DataTable _dt = new DataTable("tblStudentProfile");
                SortedList _param = new SortedList();
                _param.Add("@Name", FullName);
                _dt = _db.GetDataTableBySP("usp_StudentProfileSearch", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        StudentProfile _profile = new StudentProfile();
                        FillData(_dr, _profile);
                        retValue.Add(_profile);
                    }
                }
            }
            catch (Exception Ex)
            {
                retValue.Error = new MyError();
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "StudentProfile";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "StudentProfile");
            }

            return retValue;
        }

        public StudentProfileList GetProfileList(int CompanyID)
        {
            StudentProfileList retValue = new StudentProfileList();

            try
            {
                DataTable _dt = new DataTable("tblStudentProfile");
                SortedList _param = new SortedList();
                //_param.Add("@CompanyID", CompanyID);
                _dt = _db.GetDataTableBySP("usp_StudentProfileSelectItemList", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        StudentProfile _profile = new StudentProfile();
                        FillData(_dr, _profile);
                        retValue.Add(_profile);
                    }
                }
            }
            catch (Exception Ex)
            {
                retValue.Error = new MyError();
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "StudentProfile";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "StudentProfile");
            }

            return retValue;
        }

        public StudentProfile GetProfile(int Id)
        {
            StudentProfile retValue = new StudentProfile();

            try
            {

                DataTable _dt = new DataTable("tblStudentProfile");

                SortedList _param = new SortedList();
                _param.Add("@Id", Id);
                _dt = _db.GetDataTableBySP("usp_StudentProfileSelectSingleItem", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    FillData(_dt.Rows[0], retValue);
                }
            }
            catch (Exception Ex)
            {
                retValue.Error = new MyError();
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "StudentProfile";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "StudentProfile");
            }

            return retValue;
        }

        private void FillData(DataRow aRow, StudentProfile aStudent)
        {
            aStudent.Id = Convert.ToInt32(aRow["Id"].ToString());
            aStudent.Profile = new Person.Profile();
            aStudent.Contact = new Person.Contact();
            aStudent.Error = new MyError();

            if (aRow["FirstName"] != DBNull.Value)
            {
                aStudent.Profile.FirstName = aRow["FirstName"].ToString();
            }
            else
            {
                aStudent.Profile.FirstName = string.Empty;
            }

            aStudent.Profile.LastName = aRow["LastName"].ToString();

            if (aRow["Gender"] != DBNull.Value)
            {
                aStudent.Profile.Gender = Convert.ToChar(aRow["Gender"].ToString().ToUpper());
            }
            else
            {
                aStudent.Profile.Gender = 'N';
            }

            if (aRow["DOB"] != DBNull.Value)
            {
                aStudent.Profile.DOB = Convert.ToDateTime(aRow["DOB"].ToString());
            }
            else
            {
                aStudent.Profile.DOB = DateTime.MinValue;
            }

            if (aRow["NRIC"] != DBNull.Value)
            {
                aStudent.Profile.NRIC = aRow["NRIC"].ToString();
            }
            else
            {
                aStudent.Profile.NRIC = string.Empty;
            }

            if (aRow["ContactType"] != DBNull.Value)
            {
                aStudent.Contact.ContactType = (Person.ContactType)Enum.Parse(typeof(Person.ContactType), aRow["ContactType"].ToString());
            }
            else
            {
                aStudent.Contact.ContactType = Person.ContactType.Mobile;
            }

            if (aRow["ContactNo"] != DBNull.Value)
            {
                aStudent.Contact.ContactNo = aRow["ContactNo"].ToString();
            }
            else
            {
                aStudent.Contact.ContactNo = string.Empty;
            }

            if (aRow["Extension"] != DBNull.Value)
            {
                aStudent.Contact.Extension = aRow["Extension"].ToString();
            }
            else
            {
                aStudent.Contact.Extension = string.Empty;
            }

            if (aRow["Email"] != DBNull.Value)
            {
                aStudent.Contact.Email = aRow["Email"].ToString();
            }
            else
            {
                aStudent.Contact.Email = string.Empty;
            }

            aStudent.IsActive = Convert.ToBoolean(aRow["IsActive"].ToString());

            aStudent.Error.ErrorNo = 0;
        }
    }


}
